package com.jungle.sp.daily.monitor;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class RoomMonitor {
    private static final int PORT = 12345;
    private static Map<String, Integer> roomUserCount = new HashMap<>();

    public static void main(String[] args) {
        try (ServerSocket serverSocket = new ServerSocket(PORT)) {
            System.out.println("Server is running on port " + PORT);
            while (true) {
                Socket socket = serverSocket.accept();
                new Thread(new UserHandler(socket)).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class UserHandler implements Runnable {
        private Socket socket;

        UserHandler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            String room = joinRoom(); // 假设此方法用于用户加入房间
            roomUserCount.put(room, roomUserCount.getOrDefault(room, 0) + 1);
            // 处理用户的其他行为
            notifyRoom(room); // 假设此方法用于通知用户当前房间人数
        }

        private String joinRoom() {
            // 逻辑获取用户所加入的房间
            return "Room1"; // 示例返回
        }

        private void notifyRoom(String room) {
            // 返回当前房间的用户数
            System.out.println("Room: " + room + ", Users: " + roomUserCount.get(room));
        }
    }
}